<?php
class BusinessLeadsModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("businessleads", "businesslead_id");
        $this->addField("businesslead_id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("company_id", Model::TYPE_INT, Model::MOD_FOREIGN, array("alias" => "company"));
        $this->addField("productname", Model::TYPE_VARCHAR);
        $this->addField("description", Model::TYPE_TEXT);
        $this->addField("hs4", Model::TYPE_VARCHAR);
        $this->addField("postdate", Model::TYPE_DATE);
        $this->addField("quantity", Model::TYPE_INT);
        $this->addField("unit", Model::TYPE_VARCHAR);
        $this->addField("country_origin", Model::TYPE_VARCHAR);
        $this->addField("country_dest", Model::TYPE_VARCHAR);
        $this->addField("buysell", Model::TYPE_INT);
        $this->addField("photo_id", Model::TYPE_INT);
        $this->addField("accepted", Model::TYPE_INT, Model::MOD_ACCEPT);
        $this->addField("accepted_at", Model::TYPE_DATE_ADDITION, Model::MOD_ACCEPTDATE);
        $this->addField("update", Model::TYPE_TEXT, Model::MOD_UPDATE);
    }

    public function getAlertingLeads() {
        $alertDate = date("Y-m-d", time() - 9 * 24*60*60);  // 9 nappal a hirdetes archivumba helyezese elott kell szolni emailben
        $query = "SELECT contactperson1, email1 FROM companies ".
                 "JOIN businessleads ON businessleads.company_id=companies.company_id ".
                 "WHERE accepted_at = '$alertDate'"
        ;
        $res = Database::executeQuery($query);
        return Database::getDatafromResource($res);
    }

    public function getSince($date, $condition = "") {
        $query = "SELECT * FROM businessleads ".
                 "JOIN companies ON companies.company_id=businessleads.company_id ".
                 "WHERE companies.created_at >= '$date'"
        ;
        if ($condition != "") { $query .= " AND $condition"; }
        $query .= " LIMIT 0, 10";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data;
    }

    public function getImage($type, $companyId) {
        $query = "";
        if ($type == "rotator") {
            $query = "SELECT rotator_id AS id FROM companies WHERE company_id=$companyId";
        } else if ($type == "productphoto") {
            $query = "SELECT photo_id AS id FROM businessleads WHERE company_id=$companyId";
        }
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data[0]['id'];
    }

    public function getUpdateRequests() {
        $query = "SELECT businessleads.businesslead_id, companyname FROM businessleads ".
                 "JOIN companies ON companies.company_id=businessleads.company_id ".
                 "WHERE (companies.update_request is not null AND companies.update_request <> '') OR (businessleads.update IS NOT NULL AND businessleads.update <> '')"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[$i]['id'] = $result[$i]['businesslead_id'];
            $data[$i]['cell'] = array(
                $result[$i]['companyname']
            );
        }
        return $data;
    }

    public function getUpdateRequestCount() {
        $query = "SELECT count(*) AS c  FROM (".
                    "SELECT DISTINCT companies.company_id, companies.companyname FROM businessleads ".
                    "JOIN companies ON companies.company_id=businessleads.company_id ".
                    "WHERE accepted IS NULL OR accepted <> 1".
                 ")x"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0]['c'];
    }

    public function getSellingLeads() {
        $query = "SELECT productname AS text FROM businessleads WHERE buysell=2";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data;
    }
    
    public function getBuyingLeads() {
        $query = "SELECT productname AS text FROM businessleads WHERE buysell=1";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data;
    }

    public function getLeadsByCompanyId($companyId, $preview = false) {
        $data = array();
        $alertDate = date("Y-m-d", time() - 9 * 24*60*60);  // 9 nappal a hirdetes archivumba helyezese elott kell szolni emailben
        if ($preview) {
            $query = "SELECT * FROM businessleads WHERE company_id=$companyId";
            $res = Database::executeQuery($query);
            $data = Database::getDataFromREsource($res);
            for ($i = 0; $i < count($data); $i++) {
                $preview = $this->getPreviewData($data[$i]);
                if (dateIsPast($data[$i]['postdate'], $alertDate)) {
                    $data[$i]['renew'] = 1;
                } else {
                    $data[$i]['renew'] = 0;
                }
                if (is_array($preview)) {
                    foreach ($preview as $key=>$value) {
                        if ($key == "postdate") {
                            if (dateIsPast($value, $alertDate)) {
                                $date[$i]['renew'] = true;
                            }
                        }
                        $data[$i][$key] = $value;
                    }
                }
            }
        } else {
            $query = "SELECT * FROM businessleads WHERE company_id=$companyId";
            $res = Database::executeQuery($query);
            $data = Database::getDataFromREsource($res);
            for ($i = 0; $i < count($data); $i++) {
                if (dateIsPast($data[$i]['postdate'], $alertDate)) {
                    $data[$i]['renew'] = 1;
                } else {
                    $data[$i]['renew'] = 0;
                }
            }
        }
        return $data;
    }
/*
    public function acceptChanges($businesslead_id) {
        $query = "UPDATE businessleads SET accepted=1 WHERE businesslead_id=$businesslead_id";
        Database::executeQuery($query);
    }

    public function rejectChanges($businesslead_id) {
        $query = "DELETE FROM businessleads WHERE businesslead_id=$businesslead_id";
        Database::executeQuery($query);
    }
*/
    public function renewLeadByName($companyId, $lead) {
        $query = QueryHelper::update("businessleads", array(postdate => date("Y-m-d")), "company_id=$companyId AND productname='$lead'");
        Database::executeQuery($query);
    }

    public function saveUpdate($fieldset, $companyId, $blId) {
        unset($fieldset['businesslead_id']);
        $result = "";
        $changes = json_encode($fieldset);
        if ($blId == "") {
            $query = "INSERT INTO businessleads (company_id, `update`) VALUES ($companyId, '$changes')";
            Database::executeQuery($query);
            $result = Database::getInsertedId();
        } else {
            $query = "UPDATE businessleads SET `update`='$changes' WHERE company_id=$companyId AND businesslead_id=$blId";
            Database::executeQuery($query);
            $result = $blId;
        }
        return $result;
    }

    public function getPreview($id) {
        $query = "SELECT * FROM businessleads ".
                 "JOIN companies ON companies.company_id=businessleads.company_id ".
                 "WHERE businesslead_id=$id"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        $result = $this->getPreviewData($data[0]);
        if (count($result) > 0) {
            $result['company_id'] = $data[0]['company_id'];
        } else {
            $result = $data[0];
        }
        return $result;
    }

    public function getChanges($company_id) {
        $query = "SELECT * FROM businessleads WHERE (accepted <> 1 OR accepted IS NULL) AND company_id=$company_id";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data;
    }

    public function getCompanyId($id) {
        $query = "SELECT company_id FROM businessleads WHERE businesslead_id=$id";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0]['company_id'];
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
